package com.five.welcome.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.welcome.entity.Dormitoryallocation;
import com.five.welcome.dao.DormitoryallocationMapper;
import com.five.welcome.entity.statistic.RoomOfStudentCount;
import com.five.welcome.pojo.Rooms;
import com.five.welcome.service.DormitoryallocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.five.welcome.service.StudentinfoService;
import com.five.welcome.service.WelcomeProgressService;
import com.five.welcome.utils.SNumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ywt
 * @since 2024-07-12
 */
@Service
public class DormitoryallocationServiceImpl extends ServiceImpl<DormitoryallocationMapper, Dormitoryallocation> implements DormitoryallocationService {

    @Autowired
    private DormitoryallocationMapper dormitoryallocationMapper;

    @Autowired
    private StudentinfoService studentinfoService;


    @Autowired
    private WelcomeProgressService welcomeProgressService;

    //查询全部宿舍信息
    @Override
    public List<Dormitoryallocation> getAll() {
        QueryWrapper<Dormitoryallocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        return dormitoryallocationMapper.selectList(queryWrapper);
    }

    //通过学号查询宿舍信息
    @Override
    public Dormitoryallocation getDormitoryBySNumber(String sNumber) {
        QueryWrapper<Dormitoryallocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s_number", sNumber);
        return dormitoryallocationMapper.selectOne(queryWrapper);
    }

    //分配宿舍
    @Override
    @Transactional
    public Map<String, Object> giveRoomBySNumber(String sNumber) {
        if (getDormitoryBySNumber(sNumber) != null) {
            return null;
        }
        Random r = new Random();
        //1,2,3,4
        int buildingNo = r.nextInt(4) + 1;
        int bedNo = r.nextInt(4) + 1;
        List<Integer> rooms = Rooms.init();
        Integer room = rooms.get(r.nextInt(rooms.size()));
        int dormitoryNumber = buildingNo * 100 + room;
        Map<String, Object> map = new HashMap<>();
        map.put("building_number", buildingNo);
        map.put("dormitory_number", dormitoryNumber);
        map.put("bed_number", bedNo);
        String location = "天津市大学软件学院" + buildingNo + "号楼";
        //插入数据库
        dormitoryallocationMapper.giveRoom(sNumber, studentinfoService.getSNameBySNumber(sNumber), buildingNo, dormitoryNumber, bedNo, location);
        return map;
    }

    //分页查询宿舍数据
    @Override
    public Page<Dormitoryallocation> getAllDormitoryPage(Integer pageNumber, Integer pageSize,
                                                         String sNumber, String sname,
                                                         Integer buildingNumber, Integer dormitoryNumber) {
        Page<Dormitoryallocation> page = Page.of(pageNumber, pageSize);
        QueryWrapper<Dormitoryallocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(sNumber != null, "s_number", sNumber)
                .eq(sname != null, "s_name", sname)
                .eq(buildingNumber != null, "building_number", buildingNumber)
                .eq(dormitoryNumber != null, "dormitory_number", dormitoryNumber);
        page(page, queryWrapper);
        return page;
    }

    //统计各个宿舍楼的学生人数
    @Override
    public List<RoomOfStudentCount> RoomOfStudentCount() {
        return dormitoryallocationMapper.RoomOfStudentCount();
    }

    //注册完信息后在宿舍表先加入学号姓名
    @Override
    public int addRoom(String sNumber, String sName) {
        return dormitoryallocationMapper.addRoom(sNumber, sName);
    }

    //选择宿舍
    @Override
    public int selectRoom(String roomBuilding, String roomNumber, String bedNumber,String sNumber) {
        String location = "天津市大学软件学院" + roomBuilding + "号楼";
        //插入学生选的楼号宿舍号床位号
        UpdateWrapper<Dormitoryallocation> wrapper = new UpdateWrapper<>();
        wrapper.set("building_number", roomBuilding)
                .set("dormitory_number", roomNumber)
                .set("bed_number", bedNumber)
                .set("location", location)
                .eq("s_number", sNumber);
        int update = dormitoryallocationMapper.update(wrapper);
        if (update > 0) {
            //如果插入成功,修改迎新进度
            welcomeProgressService.updateValue("roomed", sNumber);
            return update;
        }
        return update;
    }
}
